package com.arch.dev.client.view.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

public class BudgetToolBar extends HorizontalPanel {

	BudgetImageBundle bundle = (BudgetImageBundle) GWT

	.create(BudgetImageBundle.class);

	public static final int ADDRECORD = 0;

	public static final int EDITRECORD = 1;

	public static final int DELETERECORD = 2;

	// private Widget[] widgets;

	private List<Widget> widgets;

	private HashMap<Integer, Widget> widgetMap = new HashMap<Integer, Widget>();

	// private Widget[] images = new Widget[3];

	private List<Widget> images = new ArrayList();

	public BudgetToolBar() {

		init();

		addStyleName("nsp-ToolBar");

	}

	private void init() {

		clear();

		setupImages();

		setWidgets(images);

		for (int i = 0; i < images.size(); i++)

		{

			widgetMap.put(new Integer(i), images.get(i));

		}

		display();

	}

	private void setupImages() {

		images.add(new ImageButton(null, bundle.getAddIcon()));

		images.add(new ImageButton(null, bundle.getModifyIcon()));

		images.add(new ImageButton(null, bundle.getDeleteIcon()));

	}

	/**
	 * 
	 * Displays the widgets
	 * 
	 * 
	 */

	public void display() {

		clear();

		HorizontalPanel hp = new HorizontalPanel();

		hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		for (Widget widget : widgets) {

			if (widget.isVisible() == true) {

				hp.add(widget);

				hp.add(new HTML("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));

			}

		}

		this.add(hp);

	}

	/**
	 * 
	 * Adds the object to the ToolBar.
	 * 
	 * 
	 * 
	 * @param toolObj
	 */

	public void addToolWidget(Widget toolObj) {

		widgets.add(toolObj);

		display();

	}

	/**
	 * 
	 * Removes the object from the ToolBar.
	 * 
	 * 
	 * 
	 * @param toolObj
	 */

	public void removeToolWidget(Widget toolObj) {

		widgets.remove(toolObj);

		display();

	}

	/**
	 * 
	 * Removes the object from the ToolBar.
	 * 
	 * 
	 * 
	 * @param toolObj
	 */

	public void removeAllWidgets() {

		widgets.clear();

		display();

	}

	/**
	 * 
	 * @return the widgets
	 */

	public List<Widget> getWidgets() {

		return widgets;

	}

	/**
	 * 
	 * @param widgets
	 * 
	 *            the widgets to set
	 */

	public void setWidgets(List<Widget> widgets) {

		this.widgets = widgets;

	}

	/**
	 * 
	 * This method sets the visibile property for widget within the toolbar.
	 * 
	 * 
	 * 
	 * @param widget
	 * 
	 *            widget whose visibible property needes to be set
	 * 
	 * @param visible
	 * 
	 *            false will make the widget invisible. By default it is true.
	 */

	public void setVisible(int widget, boolean visible) {

		Widget w = widgetMap.get(new Integer(widget));

		if (w == null) {

			return;

		}

		w.setVisible(visible);

		display();

	}

	/**
	 * 
	 * This method get visible property for widget within the toolbar.
	 * 
	 * 
	 * 
	 * @param widget
	 * 
	 *            widget whose visibible property needes to be set
	 * 
	 * @return visible false will make the widget invisible. By default it is
	 * 
	 *         true.
	 */

	public boolean isVisible(int widget) {

		Widget w = widgetMap.get(new Integer(widget));

		if (w == null) {

			return false;

		}

		return w.isVisible();

	}

	/**
	 * 
	 * This method sets the tool tip for widget within the toolbar.
	 * 
	 * 
	 * 
	 * @param widget
	 * 
	 *            widget whose tool tip property needes to be set
	 * 
	 * @param text
	 * 
	 *            String to be set as tool tip.
	 */

	public void setToolTip(int widget, String text) {

		Widget w = widgetMap.get(new Integer(widget));

		if (w == null) {

			return;

		}

		w.setTitle(text);

	}

	/**
	 * 
	 * This method sets the image icon for widget within the tool bar.
	 * 
	 * 
	 * 
	 * @param widget
	 * 
	 *            widget whose image icon needs to be set
	 * 
	 * @param imageIcon
	 * 
	 *            Image Icon to be set to this widget.
	 */

	public void setImageIcon(int widget, Image imageIcon) {

		Widget w = widgetMap.get(new Integer(widget));

		if (w == null) {

			return;

		}

		w = imageIcon;

		widgetMap.put(new Integer(widget), imageIcon);

		display();

	}

	/**
	 * 
	 * This method adds a handler to the widget within the toolbar.
	 * 
	 * 
	 * 
	 * @param widget
	 * 
	 * @param handler
	 */

	public void addClickHandler(int widget, ClickHandler handler) {

		ImageButton w = (ImageButton) widgetMap.get(new Integer(widget));

		if (w == null) {

			return;

		}

		w.addClickHandler(handler);

	}

}
